//
//  AutoReadingToolBar.m
//  ebook
//
//  Created by mt on 16/10/24.
//  Copyright © 2016年 Moton. All rights reserved.
//

/*
 *调节自动阅读的滚动速度
 */

#import "AutoScrollReadingToolBar.h"
#import "ReaderGlobalModel.h"

#define AutoReadingToolBarHeight 140
#define BtnWidth (MTScreenW-40)/3
#define BtnHeight 40
#define kDefaultSpeed [NSString stringWithFormat:@"当前速度: %ld",[ReaderGlobalModel shareModel].readerConfig.autoReadingSpeed]
#define reduceTitle   @"减速－"
#define increaseTitle @"加速＋"

@interface AutoScrollReadingToolBar ()
{
    UILabel *tempLabel;
}

@property (nonatomic,retain) UIColor *textColor;
@end

@implementation AutoScrollReadingToolBar

- (instancetype)init {
    
    self = [super initWithFrame:CGRectMake(0, MTScreenH-AutoReadingToolBarHeight, MTScreenW, AutoReadingToolBarHeight)];
    self.backgroundColor = MTConfigColor(@"MTReaderToolBar_Bg_Color");
    if (self = [super init]) {
        
        [self drawView];
    }
    return self;
}

- (void)drawView {
    
    UIButton *reduceBtn = [self btnWithFrame:CGRectMake(20, 20, BtnWidth, BtnHeight) title:reduceTitle tag:1];
    [self addSubview:reduceBtn];
    
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(reduceBtn.frame), CGRectGetMinY(reduceBtn.frame), BtnWidth, BtnHeight)];
    textLabel.text = kDefaultSpeed;
    textLabel.font = [UIFont systemFontOfSize:14.0];
    textLabel.textColor = self.textColor;
    textLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:textLabel];
    tempLabel = textLabel;
    
    UIButton *increaseBtn = [self btnWithFrame:CGRectMake(MTScreenW-CGRectGetMaxX(reduceBtn.frame), CGRectGetMinY(reduceBtn.frame), BtnWidth, BtnHeight) title:increaseTitle tag:2];
    [self addSubview:increaseBtn];
    
    
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(reduceBtn.frame)+20, MTScreenW, 0.3)];
    lineLabel.backgroundColor = MTConfigColor(@"MTReaderToolBar_btnSelected_Color");
    [self addSubview:lineLabel];
    
    UIButton *stopAutoReadingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    stopAutoReadingBtn.frame = CGRectMake(0, CGRectGetMaxY(lineLabel.frame), MTScreenW, self.height-CGRectGetMaxY(lineLabel.frame));
    [stopAutoReadingBtn setTitle:@" 停止自动阅读" forState:UIControlStateNormal];
    [stopAutoReadingBtn setImage:[UIImage imageNamed:@"BookContent_tuichuicon_dk"] forState:UIControlStateNormal];
    [stopAutoReadingBtn setTitleColor:[self textColor] forState:UIControlStateNormal];
    stopAutoReadingBtn.titleLabel.font = [UIFont systemFontOfSize:16.0];
    [stopAutoReadingBtn addTarget:self action:@selector(stopAutoReadingBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:stopAutoReadingBtn];
}

- (UIButton *)btnWithFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag {
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:self.textColor forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:16.0];
    btn.layer.borderColor = self.textColor.CGColor;
    btn.layer.borderWidth = 1.0f;
    btn.layer.cornerRadius = 3.0f;
    
    btn.tag = tag;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

-(UIColor *)textColor{
    return MTConfigColor(_isNight?@"MTReaderBar_NightNomarlText_Color":@"MTReaderBar_DayNomarlText_Color");
}

//加速按钮、减速按钮  点击
- (void)btnClick:(UIButton *)btn {
    
    NSInteger speed = [ReaderGlobalModel shareModel].readerConfig.autoReadingSpeed;
    if (1==btn.tag) {
        if (speed>1) {
            speed--;
        }
    }
    if (2==btn.tag) {
        if (speed<10) {
            speed++;
        }
    }
    [ReaderGlobalModel shareModel].readerConfig.autoReadingSpeed = speed;
    [self resetAutoScrollReadingSpeed:speed];
    if ([self.delegate respondsToSelector:@selector(autoScrollReadingToolBar:currentSpeed:)]) {
        [self.delegate autoScrollReadingToolBar:self currentSpeed:speed];
    }
}

//停止自动阅读 点击
- (void)stopAutoReadingBtnClick:(UIButton *)btn {
    
    if ([self.delegate respondsToSelector:@selector(autoScrollReadingToolBar:stopBtnClick:)]) {
        [self.delegate autoScrollReadingToolBar:self stopBtnClick:btn];
    }
}

- (void)resetAutoScrollReadingSpeed:(NSInteger)speed {
    
    NSString *str = [NSString stringWithFormat:@"当前速度: %ld", speed];
    tempLabel.text = str;
}

-(void)showOnView:(UIView *)view {
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect origFrame = self.frame;
        origFrame.origin.y = MTScreenH;
        origFrame.origin.y = origFrame.origin.y - CGRectGetHeight(origFrame);
        self.frame = origFrame ;
        [view addSubview:self];
        [view bringSubviewToFront:self];
    }];
}

-(void)hide {
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect origFrame = self.frame;
        origFrame.origin.y = origFrame.origin.y + CGRectGetHeight(origFrame);
        self.frame = origFrame ;
    }];
}

-(void)setIsNight:(BOOL)isNight{
    
    _isNight = isNight;
    
    self.backgroundColor =  MTConfigColor(_isNight?@"MTReaderBar_NightBg_Color":@"MTReaderBar_DayBg_Color");
}


@end
